package com.atguigu.flink.chapter07;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.util.KKutil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.time.Duration;
import java.util.List;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/12/15 9:26
 */
public class Flink14_Watermark_Sideout {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);

        env.setParallelism(1);

        SingleOutputStreamOperator<String> mainStraem = env
                .socketTextStream("hadoop162", 9999)
                .map(line -> {
                    String[] strings = line.split(",");
                    return new WaterSensor(strings[0], Long.valueOf(strings[1]), Integer.valueOf(strings[2]));
                })
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(1))
                                .withTimestampAssigner((element, ts) -> element.getTs())
                )
                .keyBy(WaterSensor::getId)
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))

                //窗口关闭之后，进来的数据，统统都进入这个侧输出流
                .sideOutputLateData(new OutputTag<WaterSensor>("late") { })

                .process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
                    @Override
                    public void process(String value, Context ctx, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {
                        List<WaterSensor> list = KKutil.toList(elements);
                        out.collect(value + "  " + elements);
                    }
                });
        mainStraem.print("nomal");
        mainStraem.getSideOutput(new OutputTag<WaterSensor>("late") { }).print("late11");


        env.execute();
    }
/*
*       flink 如何做到数据不丢失
*       1.事件时间 + 水印
*       2，允许迟到
*       3.侧输出流
*
* */


}
